/*****************************************************************
**                                                             	**
**  Copyright (C) 2004 Amlogic,Inc.                            	**
**  All rights reserved                                        	**
**        Filename : aw_user_init.c /Project:AVOS demo		        **
**        Revision : 1.1                                       	**
**                                                             	**
*****************************************************************/

#include "aw_user_init.h"
#include "menu.h"
#include "aw_init_display.h"
#include "resource.h"
#include "datasrc.h"
#include "aw_config.h"
#include "user.h"
#ifdef ENABLE_WATCHDOG
#include <Drivers/watchdog/watchdog.h>
#endif


HWND	gRootWin;			/* root window */

#define GUI_MEDIA_TYPE					MEDIA_GUI
#define GUI_TASK_INIT_STATUS			APP_READY
#define GUI_AUTO_RUN_PRIORITY			0
#define GUI_TASK_KEY_PROCESS_PRIORITY	128
#define GUI_TASK_PRIORITY				20
#define GUI_TASK_STACK_SIZE				0x800
#define GUI_TASK_MEM_MAP				MEMMAP_DEFAULT

#define SYS_MEDIA_TYPE					MEDIA_SYSTEM
#define SYS_TASK_INIT_STATUS			APP_READY
#define SYS_AUTO_RUN_PRIORITY			0
#define SYS_TASK_KEY_PROCESS_PRIORITY	1 //lowest
#define SYS_TASK_PRIORITY				17
#define SYS_TASK_STACK_SIZE				0x800
#define SYS_TASK_MEM_MAP				MEMMAP_DEFAULT

#define AVI_MEDIA_TYPE					MEDIA_AVI
#define AVI_TASK_INIT_STATUS			APP_READY
#define AVI_AUTO_RUN_PRIORITY			128
#define AVI_TASK_KEY_PROCESS_PRIORITY	64
#define AVI_TASK_PRIORITY				18
#define AVI_TASK_STACK_SIZE				0x800
#define AVI_TASK_MEM_MAP				MEMMAP_DEFAULT

#ifdef SUPPORT_RM
#define RM_MEDIA_TYPE                   MEDIA_REAL
#define RM_TASK_INIT_STATUS             APP_READY
#define RM_AUTO_RUN_PRIORITY            128
#define RM_TASK_KEY_PROCESS_PRIORITY    64
#define RM_TASK_PRIORITY                18
#define RM_TASK_STACK_SIZE              0x1000
#define RM_TASK_MEM_MAP	                MEMMAP_DEFAULT
#endif

#define VOB_MEDIA_TYPE					MEDIA_MPEG2
#define VOB_TASK_INIT_STATUS			APP_READY
#define VOB_AUTO_RUN_PRIORITY			128
#define VOB_TASK_KEY_PROCESS_PRIORITY	64
#define VOB_TASK_PRIORITY				18
#define VOB_TASK_STACK_SIZE				0x1000
#define VOB_TASK_MEM_MAP				MEMMAP_DEFAULT

#define PIC_MEDIA_TYPE					MEDIA_PIC
#define PIC_TASK_INIT_STATUS			APP_READY
#define PIC_AUTO_RUN_PRIORITY			128
#define PIC_TASK_KEY_PROCESS_PRIORITY	64
#define PIC_TASK_PRIORITY				19
#define PIC_TASK_STACK_SIZE				0x800
#define PIC_TASK_MEM_MAP				PIC_MEMMAP_DEFAULT

#define GPLAYER_MEDIA_TYPE					MEDIA_GENERAL
#define GPLAYER_TASK_INIT_STATUS			APP_READY
#define GPLAYER_AUTO_RUN_PRIORITY			128
#define GPLAYER_TASK_KEY_PROCESS_PRIORITY	64
#define GPLAYER_TASK_PRIORITY				18
#define GPLAYER_TASK_STACK_SIZE				0x800
#define GPLAYER_TASK_MEM_MAP				MEMMAP_DEFAULT

#define TXT_MEDIA_TYPE					MEDIA_TXT
#define TXT_TASK_INIT_STATUS			APP_READY
#define TXT_AUTO_RUN_PRIORITY			128
#define TXT_TASK_KEY_PROCESS_PRIORITY	64
#define TXT_TASK_PRIORITY				18
#define TXT_TASK_STACK_SIZE				0x800
#define TXT_TASK_MEM_MAP				MEMMAP_DEFAULT

#ifdef ENABLE_NETWORK_BASIC
#include "aw_network.h"
#include "net_app.h"
extern struct aml_iw_config     aml_wireless_config;
extern struct aml_netif_setting aml_network_config;

extern u8_t yahoo_auto_enable;
extern u8_t google_auto_enable;
extern u8_t framechannel_auto_enable;

extern yahoo_login_info_t			yahoo_config;
extern google_login_info_t			google_config;
extern framechannel_login_info_t	framechannel_config;
#endif

#ifdef ENABLE_NETWORK
#define NET_MEDIA_TYPE					MEDIA_NET
#define NET_TASK_INIT_STATUS			APP_READY
#define NET_AUTO_RUN_PRIORITY			0
#define NET_TASK_KEY_PROCESS_PRIORITY	1 //lowest
#define NET_TASK_PRIORITY				19
#define NET_TASK_STACK_SIZE				0x800
#define NET_TASK_MEM_MAP				MEMMAP_DEFAULT

#include "aw_network.h"
extern struct aml_iw_config     aml_wireless_config;
extern struct aml_netif_setting aml_network_config;
extern u8_t yahoo_auto_enable;
extern u8_t google_auto_enable;

extern yahoo_login_info_t yahoo_config;
extern google_login_info_t google_config;

int GetWifiConfigCache(struct iw_config *config)
{
#if 1
    AVSchedLock();
    memset(config, 0, sizeof(*config));

    config->version  = WIRELESS_CONFIG_VERSION;
    config->magic    = WIRELESS_CONFIG_MAGIC;
    config->mode     = IW_MODE_INFRA;
    config->security = 2;
    strncpy(config->essid, "SMC", IW_ESSID_MAX_SIZE);
    config->key_len  = 8;
    memcpy(config->key, "12344321", IW_ENCODING_TOKEN_MAX);
    AVSchedUnlock();

    return TRUE;
#else
    AVSchedLock();
    memset(config, 0, sizeof(*config));

    config->version  = WIRELESS_CONFIG_VERSION;
    config->magic    = WIRELESS_CONFIG_MAGIC;
    config->mode     = aml_wireless_config.mode;
    config->security = aml_wireless_config.security;
    config->key_len  = aml_wireless_config.key_len;
    strncpy(config->essid, aml_wireless_config.essid, IW_ESSID_MAX_SIZE);
    memcpy(config->key, aml_wireless_config.key, IW_ENCODING_TOKEN_MAX);
    AVSchedUnlock();
#endif
}

struct netif_setting aml_net_config;
int GetNetConfigCache(struct netif_setting *config)
{
#if 0
    AVSchedLock();
    memset(config, 0, sizeof(*config));

    config->version              = NETWORK_CONFIG_VERSION;
    config->magic                = NETWORK_CONFIG_MAGIC;
    config->status               = 1; //aml_net_config.status;
    config->dhcp                 = 1; //aml_net_config.dhcp;
    config->ipaddr.s_addr        = 0; //aml_net_config.ipaddr.s_addr;
    config->netmask.s_addr       = 0; //aml_net_config.netmask.s_addr;
    config->gateway.s_addr       = 0; //aml_net_config.gateway.s_addr;
    config->primary_dns.s_addr   = 0; //aml_net_config.primary_dns.s_addr;
    config->secondary_dns.s_addr = 0; //aml_net_config.secondary_dns.s_addr;
    AVSchedUnlock();
#else
    AVSchedLock();
    memset(config, 0, sizeof(*config));

    config->version              = NETWORK_CONFIG_VERSION;
    config->magic                = NETWORK_CONFIG_MAGIC;
    config->status               = aml_network_config.status;
    config->dhcp                 = aml_network_config.dhcp;
    config->ipaddr.s_addr        = aml_network_config.ipaddr.s_addr;
    config->netmask.s_addr       = aml_network_config.netmask.s_addr;
    config->gateway.s_addr       = aml_network_config.gateway.s_addr;
    config->primary_dns.s_addr   = aml_network_config.primary_dns.s_addr;
    config->secondary_dns.s_addr = aml_network_config.secondary_dns.s_addr;
    AVSchedUnlock();
#endif

    return TRUE;
}

int SetNetConfigCache(struct netif_setting *config)
{
#if 0
    AVSchedLock();
    aml_net_config.status               = config->status;
    aml_net_config.dhcp                 = config->dhcp;
    aml_net_config.ipaddr.s_addr        = config->ipaddr.s_addr;
    aml_net_config.netmask.s_addr       = config->netmask.s_addr;
    aml_net_config.gateway.s_addr       = config->gateway.s_addr;
    aml_net_config.primary_dns.s_addr   = config->primary_dns.s_addr;
    aml_net_config.secondary_dns.s_addr = config->secondary_dns.s_addr;
    AVSchedUnlock();
#else
    AVSchedLock();
    aml_network_config.status               = config->status;
    aml_network_config.dhcp                 = config->dhcp;
    aml_network_config.ipaddr.s_addr        = config->ipaddr.s_addr;
    aml_network_config.netmask.s_addr       = config->netmask.s_addr;
    aml_network_config.gateway.s_addr       = config->gateway.s_addr;
    aml_network_config.primary_dns.s_addr   = config->primary_dns.s_addr;
    aml_network_config.secondary_dns.s_addr = config->secondary_dns.s_addr;
    AVSchedUnlock();

    return SetNetConfig(&aml_network_config);
#endif
}
#endif


void RegisterAllUseApp()
{
	AWApplicationRegister(GUI_MEDIA_TYPE, GUI_TASK_INIT_STATUS,GUI_AUTO_RUN_PRIORITY, GUI_TASK_KEY_PROCESS_PRIORITY, 
             GUI_TASK_PRIORITY, GUI_TASK_MEM_MAP, GUI_TASK_STACK_SIZE, AWGUIMsgProcess, AWGUIGetInfo, AWWinMain);
	AWApplicationRegister(SYS_MEDIA_TYPE, SYS_TASK_INIT_STATUS,SYS_AUTO_RUN_PRIORITY, SYS_TASK_KEY_PROCESS_PRIORITY	,
	         SYS_TASK_PRIORITY, SYS_TASK_MEM_MAP, SYS_TASK_STACK_SIZE, AWSysMsgProcess, AWSysGetInfo, AWSysTaskProcess) ;
	AWApplicationRegister(AVI_MEDIA_TYPE,AVI_TASK_INIT_STATUS,AVI_AUTO_RUN_PRIORITY,AVI_TASK_KEY_PROCESS_PRIORITY,
		         AVI_TASK_PRIORITY,AVI_TASK_MEM_MAP,AVI_TASK_STACK_SIZE,AWAVIMsgProcess,AVIGetInfo,AVITaskProcess);
	AWApplicationRegister(VOB_MEDIA_TYPE,VOB_TASK_INIT_STATUS,VOB_AUTO_RUN_PRIORITY,VOB_TASK_KEY_PROCESS_PRIORITY,
		         VOB_TASK_PRIORITY,VOB_TASK_MEM_MAP,VOB_TASK_STACK_SIZE,AWVOBMsgProcess,VOBGetInfo,VOBTaskProcess);
#ifdef SUPPORT_RM
	AWApplicationRegister(RM_MEDIA_TYPE,RM_TASK_INIT_STATUS,RM_AUTO_RUN_PRIORITY,RM_TASK_KEY_PROCESS_PRIORITY,
		         RM_TASK_PRIORITY,RM_TASK_MEM_MAP,RM_TASK_STACK_SIZE,AWRMMsgProcess,RMGetInfo,RMTaskProcess);
#endif
	AWApplicationRegister(PIC_MEDIA_TYPE,PIC_TASK_INIT_STATUS,PIC_AUTO_RUN_PRIORITY,PIC_TASK_KEY_PROCESS_PRIORITY,
		         PIC_TASK_PRIORITY,PIC_TASK_MEM_MAP,PIC_TASK_STACK_SIZE,AWPICMsgProcess,PICAppGetInfo,PICTaskProcess);
	AWApplicationRegister(GPLAYER_MEDIA_TYPE,GPLAYER_TASK_INIT_STATUS,GPLAYER_AUTO_RUN_PRIORITY,GPLAYER_TASK_KEY_PROCESS_PRIORITY,
		         GPLAYER_TASK_PRIORITY,GPLAYER_TASK_MEM_MAP,GPLAYER_TASK_STACK_SIZE,AWGPLAYERMsgProcess,gPlayerAppGetInfo,gPlayerTaskProcess);
	AWApplicationRegister(TXT_MEDIA_TYPE,TXT_TASK_INIT_STATUS,TXT_AUTO_RUN_PRIORITY,TXT_TASK_KEY_PROCESS_PRIORITY,
		         TXT_TASK_PRIORITY,TXT_TASK_MEM_MAP,TXT_TASK_STACK_SIZE,AWTXTMsgProcess,TxtAppGetInfo,TxtTaskProcess);
#ifdef ENABLE_NETWORK
	AWApplicationRegister(NET_MEDIA_TYPE, NET_TASK_INIT_STATUS,NET_AUTO_RUN_PRIORITY, NET_TASK_KEY_PROCESS_PRIORITY	,
	         NET_TASK_PRIORITY, NET_TASK_MEM_MAP, NET_TASK_STACK_SIZE, AWNetMsgProcess, AWSysGetInfo, AWNetTaskProcess) ;
	netif_setting_func_init(GetNetConfigCache, SetNetConfigCache);
	wireless_setting_func_init(GetWifiConfigCache, NULL);

	/* If fail to read, reset to factory default. */
	if (!GetNetConfig(&aml_network_config)) {
		memset(&aml_network_config, 0, sizeof(aml_network_config));
		aml_network_config.status  = 1;
		aml_network_config.dhcp    = 1;
		SetNetConfig(&aml_network_config);
	}

	 /* If fail to read or read in garbage, reset to factory default. */
	if (!GetWifiConfig(&aml_wireless_config)) {
		memset(&aml_wireless_config, 0, sizeof(aml_wireless_config));
		aml_wireless_config.mode = IW_MODE_INFRA;
		SetWifiConfig(&aml_wireless_config);
	}
#endif

    return;
}
//Init Font Section 

//#include "font_english_20.h"
#ifdef BIG5
//#include "font_Chinese_big5l_24.h"
#include "font_Chinese_big5_20.h"
#else 
#ifdef TV_MODE_1080P
#include "English_Arial_24.h"
#else
#include "font_chinese_kai_20.h"
#endif
#endif
				
void SetENFont(void)
{
//extern char MsgEnglish_mem[] ;
//						AWRegisterFont(&gEnglishFont20) ;
//			            AWResourceRegister((OSD_LANGUAGE_STRING >> 16), "/dev/flash/MsgEnglish.res",MsgEnglish_mem);
}
void InitFont(void)
{
	AWSetForeground(3);
	AWSetBackground(0);
// AWRegisterFont(&gEnglishFont20) ;
#ifdef BIG5
	//AWRegisterFont(&gFont_chinese_big5_24Font);
	AWRegisterFont(&gFont_cbig5_20Font);
#else 
#ifdef TV_MODE_1080P
	AWRegisterFont(&gEnglish_arial_24Font);
#else
	AWRegisterFont(&gChineseFontK20);
#endif
#endif
    return;
}
//user initial function 

#ifdef ENABLE_WATCHDOG
void enable_hw_watchdog(void)
{
    int fd;
    fd = open("/dev/hw_watchdog", O_RDONLY);
    if (fd < 0) 
        return;
/*
 enable hardware watchdog, time limit is 30s , can from 1 to 40
 call ioctl(fd,IOCMD_WATCHDOG_STOP) when you want to stop it
*/
    ioctl(fd,IOCMD_WATCHDOG_START_AUTO,30);
    close(fd);
    
}

void disable_hw_watchdog(void)
{
    int fd;
    fd = open("/dev/hw_watchdog", O_RDONLY);
    if (fd < 0) 
        return;
    ioctl(fd,IOCMD_WATCHDOG_STOP,0);
    close(fd);    
}    
#endif
void AWWinUserDeviceInitial(void)
{
    /* init all other special devices */
    InitOtherDevs();	
}


void AWWinUserInitial(void)
{
#ifdef ENABLE_NANDDRV
#ifdef NAND_BOOT    
		nand_start_boot();
#endif
#endif
    Init_I2C_parameters();

	if(!GetParaInit())
#ifdef MALATA_VERSION
	SetTVOutPutModePara(4);
#else
	SetTVOutPutModePara(0);
#endif
    AWInitDisplay();
    //open remote/panel device
    RegisterAllInputDev() ;
    //open vfd device

    //init resource
    InitResource();
    //init font lib
    InitFont() ;
    //initial window
    RegisterAllUseControl() ;

    gRootWin.parent = NULL ;
    gRootWin.children = NULL ;
    gRootWin.siblings = NULL ;
    strcpy(gRootWin.win_name,"_AWWindowsRoot_") ;
    gRootWin.WinProc = AWDefWinProc ;
    gRootWin.winrect.left = 0 ;
    gRootWin.winrect.top = 0 ;
    gRootWin.winrect.width = 1920 ;
    gRootWin.winrect.height = 1080 ;
    gRootWin.pClass = NULL ;
    gRootWin.style = WS_FOCUSABLE ;

    gpRootWin = &gRootWin ;
    gpFocusWin = &gRootWin ;

    //create GUI and system app
    RegisterAllUseApp() ;

    InitFileSystem() ;
	DataSrcEnableMemory();
    AWSetDevUpdateStatus(1) ;

    /* init all other special device which not auto generate by tool*/
	AWSetDeviceInitCB(AWWinUserDeviceInitial);
	
    //InitOtherDevs();
	

	//open video display device
#ifdef ENABLE_WATCHDOG
	enable_hw_watchdog();
#endif

    return ;
}
